﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.FailureServiceReference;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system.Tools;
using System.Collections.ObjectModel;
using bdnt_pets_system_Entity;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.FailuresTraking
{
    public partial class TrakingDescrWindow : BaseWindow
    {
        FailureServiceReference.FailureServiceClient faiClient;
        DataServiceReference.DataServiceClient dataClient;
        Tbl_FailureTracking CuTbl_FailureTracking
        {
            get { return this.DataContext as Tbl_FailureTracking; }
            set { this.DataContext = value; }
        }

        public TrakingDescrWindow(Tbl_FailureTracking cuObj)
        {
            InitializeComponent();
            this.Title = MyStrings.gzzz;

            fvAttach.CuAttachment.TrakingId = cuObj.Id;
            SelectAttach();

            CuTbl_FailureTracking = cuObj;
            fvDesc.CuTrakingId = CuTbl_FailureTracking.Id;

            dataClient = new DataServiceReference.DataServiceClient();
            faiClient = new FailureServiceClient();
            InitEvent();
        }

        void SelectAttach()
        {
            fvAttach.Tag = this;
            fvAttach.QueryData();
        }

        void InitEvent()
        {
            faiClient.UpdateTrackingCompleted += new EventHandler<UpdateTrackingCompletedEventArgs>(faiClient_UpdateTrackingCompleted);

            hbXztd.Click += new RoutedEventHandler(hbXztd_Click);
            faiClient.OverTrackingCompleted += new EventHandler<OverTrackingCompletedEventArgs>(faiClient_OverTrackingCompleted);
            faiClient.GetTrakingDescrByTrakingIdCompleted += new EventHandler<GetTrakingDescrByTrakingIdCompletedEventArgs>(faiClient_GetTrakingDescrByTrakingIdCompleted);
            btnOverTracking.Click += new RoutedEventHandler(btnOverTracking_Click);
            btnEditTracking.Click += new RoutedEventHandler(btnEditTracking_Click);
            btnSaveTracking.Click += new RoutedEventHandler(btnSaveTracking_Click);
            InitData();
        }

        void hbXztd_Click(object sender, RoutedEventArgs e)
        {
            AddToTrakingWindow atw = new AddToTrakingWindow();
            atw.Show();
            atw.Closed += new EventHandler(atw_Closed);
        }
        void atw_Closed(object sender, EventArgs e)
        {
            var cuo = sender as AddToTrakingWindow;
            if (cuo.DialogResult == true)
            {
                CuTbl_FailureTracking.TeamId = cuo.zzTdId;
                CuTbl_FailureTracking.TeamName = cuo.zzTdName;
                CuTbl_FailureTracking.ToEmpNames = cuo.selectMembersName;
                if (cuo.selectMemembers != null && cuo.selectMemembers.Count > 0)
                {
                    CuTbl_FailureTracking.ToEmpIds = string.Join(",", cuo.selectMemembers);
                }
            }
        }
        void faiClient_UpdateTrackingCompleted(object sender, UpdateTrackingCompletedEventArgs e)
        {
            if (e.Result)
            {
                btnEditTracking.Visibility = tbZzTd.Visibility = tbTrackingEndDate.Visibility = System.Windows.Visibility.Visible;
                btnSaveTracking.Visibility = hbXztd.Visibility = dpEndTrakingDate.Visibility = System.Windows.Visibility.Collapsed;
                tbErrors.Text = "";
            }
            else
                tbErrors.Text = MyStrings.bcycqcxcs;// "保存异常，请重新尝试！";
        }

        void btnSaveTracking_Click(object sender, RoutedEventArgs e)
        {
            if (CuTbl_FailureTracking.TeamId == 0)
            {
                tbErrors.Text = MyStrings.qxzzztd;// "请选择追踪团队";
                return;
            }

            if (string.IsNullOrEmpty(CuTbl_FailureTracking.ToEmpIds))
            {
                tbErrors.Text = MyStrings.qxzzrr;// "请选择责任人";
                return;
            }

            if (!CuTbl_FailureTracking.TrackingEndDate.HasValue)
            {
                tbErrors.Text = MyStrings.qxzzzjsrq;// "请选择追踪结束日期";
                return;
            }
            faiClient.UpdateTrackingAsync(CuTbl_FailureTracking);
        }

        void btnEditTracking_Click(object sender, RoutedEventArgs e)
        {
            btnEditTracking.Visibility = tbZzTd.Visibility = tbTrackingEndDate.Visibility = System.Windows.Visibility.Collapsed;
            btnSaveTracking.Visibility = hbXztd.Visibility = dpEndTrakingDate.Visibility = System.Windows.Visibility.Visible;
        }

        void faiClient_OverTrackingCompleted(object sender, OverTrackingCompletedEventArgs e)
        {
            if (e.Result)
                this.DialogResult = true;
        }

        void btnOverTracking_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyjszz, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            if (CuTbl_FailureTracking == null)
                return;
            faiClient.OverTrackingAsync(CuTbl_FailureTracking.Id);
        }

        void InitData()
        {
            faiClient.GetTrakingDescrByTrakingIdAsync(CuTbl_FailureTracking.Id);
        }
        void faiClient_GetTrakingDescrByTrakingIdCompleted(object sender, GetTrakingDescrByTrakingIdCompletedEventArgs e)
        {
            if (e.Result == null)
                //fvDesc.CuTrakingId = CuTbl_FailureTracking.Id;
                fvDesc.BindData(new ObservableCollection<Tbl_FailureTrackingDescr>());
            else
            {
                //fvDesc.CuTrakingId = CuTbl_FailureTracking.Id;
                fvDesc.BindData(e.Result);

            }
        }
    }
}

